minimize the maximum difference of pairs solution in java